<?php
declare (strict_types = 1);

namespace app\controller\backend\user;

use app\exception\LogicException;
use app\model\communal\ImMessageModel;
use app\service\logic\common\chat\FriendsService;
use app\service\logic\common\chat\ChatService;
use app\validate\common\user\ChatValidate;
use think\App;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use app\controller\backend\BackendController;
use think\response\Json;

class ChatController extends BackendController
{
    protected $service;

    public function __construct(App $app, ChatService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 获取聊天人列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, ChatValidate::class . '.index');
        $admin = auth('backend')->userOrFail();
        $search['login_id'] = $admin['id'];
        $search['login_type'] = ImMessageModel::MORPH_TYPE_ONE;

        $list = $this->service->getList($params, $search_fields, $search, ['sender', 'receiver']);
        $total = $this->service->getCount($search_fields, $search);

        return amis([
            'items' => $list,
            'total' => $total,
        ]);
    }

    /**
     * 获取聊天人列表(无分页)
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function no()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);
        $admin = auth('backend')->userOrFail();
        $search['login_id'] = $admin['id'];
        $search['login_type'] = ImMessageModel::MORPH_TYPE_ONE;

        $list = $this->service->getList($params, $search_fields, $search, ['sender', 'receiver'], null, false);
        $total = $this->service->getCount($search_fields, $search);

        return amis([
            'items' => $list,
            'total' => $total,
        ]);
    }

    /**
     * 我的好友列表(-!-就是互相聊过天的,目前为客服用户互撩)
     *
     * @return Json
     */
    public function friends()
    {
        $params = $this->request->param();
        $search_fields = invoke(FriendsService::class)->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, ChatValidate::class . '.friends');
        $admin = auth('backend')->userOrFail();
        $search['admin_id'] = $admin['id'];

        $result = invoke(FriendsService::class)->getFriendsByAdmin($params, $search);
        return amis($result);
    }

    /**
     * 已读
     *
     * @return Json
     */
    public function already()
    {
        $params = $this->request->post();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $admin = auth('backend')->userOrFail(); // 接收人
        $search['receiver_id'] = $admin['id'];
        $search['receiver_type'] = ImMessageModel::MORPH_TYPE_ONE;
        $search['state'] = ImMessageModel::STATE_ONE;
        $this->validate($params, ChatValidate::class . '.already');

        $result = $this->service->already($params, $search_fields, $search);
        return amis(['rows' => $result]);
    }

    /**
     * 发送消息
     *
     * @return Json
     * @throws LogicException
     */
    public function send()
    {
        $params = $this->request->only($this->service->getSaveColumn());
        $admin = auth('backend')->userOrFail();
        $params['sender_id'] = $admin['id'];
        $params['sender_type'] = ImMessageModel::MORPH_TYPE_ONE;
        $this->validate($params, ChatValidate::class . '.send');

        $result = $this->service->create($params);
        return amis($result);
    }
}
